package com.dyz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dyz.domain.Config;
import com.dyz.domain.WebConfig;
import com.dyz.model.Result;
import com.dyz.service.IConfigService;
import com.dyz.service.IWebConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@RestController
@RequestMapping("/config")
@Api(tags = "配置管理")
public class ConfigController {
    @Resource
    private IConfigService configService;
    @GetMapping("/getList")
    @ApiOperation(value = "获取配置列表")
    @PreAuthorize("hasAuthority('config_query')")
    public Result<?> getConfigList(@ApiIgnore Page<Config> page, String name, String type, String code){
        return configService.getConfigList(page, name, type, code);
    }
    @GetMapping("/getOneObj/{id}")
    @ApiOperation(value = "获取配置详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true),
    })
    @PreAuthorize("hasAuthority('config_query')")
    public Result<?> getOneObj(@PathVariable Long id){
        return Result.success(configService.getById(id));
    }
   @PostMapping("/update")
    @ApiOperation(value = "修改配置")
    @PreAuthorize("hasAuthority('config_update')")
    public Result<?> update(@RequestBody Config config){
        return Result.success(configService.updateById(config));
    }
    @PostMapping("/create")
    @ApiOperation(value = "添加配置")
    @PreAuthorize("hasAuthority('config_create')")
    public Result<?> create(@RequestBody Config config){
        return configService.save(config)?Result.success("添加成功"):Result.fail("添加失败");
    }
    @PostMapping("/delete")
    @ApiOperation(value = "删除配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "主键数组", required = true),
    })
    @PreAuthorize("hasAuthority('config_delete')")
    public Result<?> delete(@RequestBody String[] ids){
        return configService.removeConfigs(ids);
    }
}
